package ${t.packagePath}.controller;

import java.util.Arrays;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.mkNameBig}Service;
import ${t.packagePath}.model.${t.mkNameBig};
import ${t.packagePath}.model.magic.${t.mkNameBig}Magic;
import ${t.packagePath}.utils.AjaxJson;

/**
 * Controller: ${t.tableName} -- ${t.tableComment}
 * @author ${cfg.author} 
 */
@RestController
@RequestMapping("/${t.varName}/")
@RequiredArgsConstructor
public class ${t.mkNameBig}Controller {

	${t.mkNameBig}Service ${t.varName}Service;
	BeanSearcher searcher;
	@ApiImplicitParams({
<#list t.t1List as c>
		@ApiImplicitParam(name = "${c.fieldName}", value = "${c.columnComment}", allowableValues = "", required = false),
</#list>
	})
	@ApiOperation("增")
	@${cfg.apiMappingWayString}("add")
<#rt>
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(@RequestBody ${t.modelName} ${t.varNameSimple}){
<#rt>
		boolean b = ${t.varName}Service.save(${t.varNameSimple});
		return AjaxJson.getByBoolean(b);
	}

	@ApiOperation("删")
	@${cfg.apiMappingWayString}("delete")
	@ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "数字", allowableValues = "", required = false),
    })
<#rt>
	public AjaxJson delete(@RequestParam ${t.primaryKey.fieldType} id){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
		boolean b = ${t.varName}Service.removeById(id);
		return AjaxJson.getByBoolean(b);
	}
	
	@ApiOperation("删-批量")
	@${cfg.apiMappingWayString}("deleteByIds")
	@ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键：逗号隔开", dataType = "字符串", allowableValues = "", required = false),
    })
<#rt>
	public AjaxJson deleteByIds(@RequestParam String ids){
<#rt>
<#if t.hasFo("logic-delete")>
		int line = ${t.varName}Service.updateColumnByIds(${t.modelName}.TABLE_NAME, "${t.getDbColumnByFoType('logic-delete').columnName}", ${t.getDbColumnByFoType('logic-delete').tx.no}, ids);
<#else>
		String[] split = ids.split(",");
		boolean b = ${t.varName}Service.removeByIds(Arrays.asList(split));
</#if>
		return AjaxJson.getByBoolean(b);
	}

	@ApiOperation("改")
	@${cfg.apiMappingWayString}("update")
	@ApiImplicitParams({
<#list t.t1List as c>
		@ApiImplicitParam(name = "${c.fieldName}", value = "${c.columnComment}", allowableValues = "", required = false),
</#list>
	})
<#rt>
	public AjaxJson update(@RequestBody ${t.modelName} ${t.varNameSimple}){
<#rt>
		boolean b = ${t.varName}Service.updateById(${t.varNameSimple});
		return AjaxJson.getByBoolean(b);
	}

<#--	@ApiOperation("改-单个字段")-->
<#--	@${cfg.apiMappingWayString}("update*")-->
<#--<#if cfg.saTokenAuthWay == 2 >    //@SaCheckPermission(type= StpXXXUtil.TYPE,value=${t.modelName}.PERMISSION_CODE)${cfg.line}</#if><#rt>-->
<#--		public AjaxJson updateColumn(@RequestParam Serializable id, @RequestParam Serializable value, HttpServletRequest request){-->
<#--<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>-->
<#--		${t.modelName} byId = ${t.varName}Service.getById(id);-->
<#--		int i = request.getRequestURI().lastIndexOf("/");-->
<#--		//根据路径获取要更新的属性名称。-->
<#--        String property = request.getRequestURI().substring(i).replace("/update", "");-->
<#--		Method method = ReflectUtil.getMethod(${t.modelName}.class, "set" + StrUtil.upperFirst(property), value.getClass());-->
<#--		if (method == null) {-->
<#--            return AjaxJson.getError("无相关属性");-->
<#--        }-->
<#--        ReflectUtil.invoke(byId, "set" + property, value);-->
<#--		return AjaxJson.getByBoolean(${t.varName}Service.updateById(byId));-->
<#--	}-->

	@ApiOperation("查")
	@${cfg.apiMappingWayString}("getById")
	@ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "数字", allowableValues = "", required = false),
    })
<#rt>
		public AjaxJson getById(@RequestParam ${t.primaryKey.fieldType} id){
		${t.modelName} ${t.varNameSimple} = ${t.varName}Service.getById(id);
		return AjaxJson.getSuccessData(${t.varNameSimple});
	}

	@ApiOperation("分页查询")
	@${cfg.apiMappingWayString}("getList")
	@ApiImplicitParams({
<#list t.t1List as c>
		@ApiImplicitParam(name = "${c.fieldName}", value = "${c.columnComment}", allowableValues = "", required = false),
</#list>
	})
<#rt>
	public AjaxJson getList(HttpServletRequest request) {
		SearchResult<${t.mkNameBig}Magic> search = searcher.search(${t.mkNameBig}Magic.class, MapUtils.flat(request.getParameterMap()));
		return AjaxJson.getSuccessData(search);
	}
}
